<template>
  <el-dialog title="编辑委托信息" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="editForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="委托事件" prop="title" label-width="110px">
        <el-input placeholder="请输入事件名称" width="120px" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="委托业务" label-width="110px" prop="business">
        <el-select
          v-model="form.business"
          filterable
          multiple
          placeholder="请选择"
          
          @change="selectBusiness"
        >
          <el-option
            v-for="item in businessData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="委托人" prop="user" label-width="110px">
        <el-select v-model="form.user" filterable placeholder="请选择" @change="selectUser">
          <el-option
            v-for="item in data"
            :key="item.objectId"
            :label="item.usrName"
            :value="item.objectId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="被委托人" prop="userd" label-width="110px">
        <el-select v-model="form.userd" filterable placeholder="请选择" @change="selectUserd">
          <el-option
            v-for="item in data"
            :key="item.objectId"
            :label="item.usrName"
            :value="item.objectId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="委托时间" prop="date" label-width="110px">
        <el-date-picker
          v-model="form.date"
          type="datetimerange"
          ref="formDate"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="委托开始时间"
          end-placeholder="委托结束时间"
          @change="changeDate"
          align="right"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否启用" prop="statuz" label-width="110px">
        <el-radio-group v-model="form.statuz">
          <el-radio v-model="form.statuz" label="启用">启用</el-radio>
          <el-radio v-model="form.statuz" label="停用">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="委托原因" prop="reason" label-width="110px">
        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.reason"></el-input>
      </el-form-item>
      <el-form-item label-width="110px">
        <el-button type="primary" @click="onSubmit('editForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import TreeSelect from "@/components/basic-treeselect/treeselect";
import { activeUrl } from "@/config/env";
import { baseUrl } from "@/config/env";
import { timestampToTime } from "@/util/date";
const urls = {
  save: activeUrl + "/delegate/delegate/edit",
  user_list: baseUrl + "/cp/api/list",
  define: activeUrl + "/activitDefineList",
  get: activeUrl + "/delegate/delegate/edit_form"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      businessData: [],
      defaultCheckedKeys: [],
      form: {
        objectId: null,
        title: null,
        business: [],
        business_json: "",
        user: null,
        userd: null,
        date: null,
        statuz: null,
        reason: null,
        userName: null,
        userdName: null
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      rules: {
        business: [
          { required: true, message: "请选择委托业务", trigger: "blur" }
        ],
        user: [{ required: true, message: "请选择委托人", trigger: "blur" }],
        userd: [{ required: true, message: "请选择被委托人", trigger: "blur" }],
        date: [{ required: true, message: "请选择委托时间", trigger: "blur" }],
        statuz: [
          { required: true, message: "请选择委托状态", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "请输入委托原因", trigger: "blur" }
        ],
        title: [{ required: true, message: "请输入委托事项", trigger: "blur" }]
      }
    };
  },
  components: {
    TreeSelect
  },
  methods: {
    popoverHide(checkedIds, checkedData) {
      this.form.menus = checkedIds.toString();
    },
    show(objectId) {
      this.dialogVisible = true;
      if (this.$refs["editForm"]) {
        this.$refs["editForm"].resetFields();
      }
      this.fetch(objectId);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.form.start = new Date(this.form.date[0]).getTime();
          this.form.end = new Date(this.form.date[1]).getTime();
          this.form.business = this.form.business_json;
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch(objectId) {
      axios.post(urls.user_list, { usrName: this.userName }).then(res => {
        this.data = res.data.result;
      });
      axios.get(urls.define).then(res => {
        this.businessData = res.data.result;
      });
      axios.get(urls.get, { params: { objectId: objectId } }).then(res => {
        var _this = this;
        this.form = res.data.result;
        this.form.business_json = res.data.result.business;
        var arr = JSON.parse(res.data.result.business);
        var business = [];
        for (var i = 0; i < arr.length; i++) {
          business.push(arr[i].id); 
        }
        this.form.business = business;
        this.$set(this.form,'date',[timestampToTime(new Date(res.data.result.start)),timestampToTime(new Date(res.data.result.end))]);
      });
    },
    changeDate(value) {
      // this.form.date = new Date(value.value);
    },
    blurDate(value) {
      // this.form.date = new Date(value.value);
    },
    selectUser(value) {
      let obj = {};
      obj = this.data.find(item => {
        return item.objectId === value;
      });
      this.form.userName = obj.usrName;
    },
    selectBusiness(value) {
      let arr = [];
      for (let i = 0; i < value.length; i++) {
        let obj = {};
        obj = this.businessData.find(item => {
          return item.id === value[i];
        });
        arr.push({ id: obj.id, name: obj.name });
      }
      this.form.business_json = JSON.stringify(arr);
    },
    selectUserd(value) {
      let obj = {};
      obj = this.data.find(item => {
        return item.objectId === value;
      });
      this.form.userdName = obj.usrName;
    }
  }
};
</script>
<style  scoped>
  /deep/.el-range-editor.el-input__inner {
    width :100%;
  }
  /deep/.el-select {
    width: 100%;
  }
  /deep/.el-date-editor--datetimerange.el-input, .el-date-editor--datetimerange.el-input__inner {
    width: 100%;
  }
</style>